$(function(){

  // 获取ulr参数
  var paramId = GetRequest().id
  function GetRequest() {  
    var url = location.search; //获取url中"?"符后的字串  
    var theRequest = new Object();  
    if (url.indexOf("?") != -1) {  
       var str = url.substr(1);  
       strs = str.split("&");  
       for(var i = 0; i < strs.length; i ++) {  
          theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
       }  
    }  
    return theRequest;
 }

 
//  随机链接软文
 var randomArr = ['https://mp.weixin.qq.com/s/K0jdWidQq1vUFdeiIl9zyQ',
                  'https://mp.weixin.qq.com/s/JDppXK2LZ64nDJ-Rxu5KXw',
                  'https://mp.weixin.qq.com/s/tJq6zNpLk6Cwau5Zt4o2yQ'
                 ]
 var randNum = Math.round(Math.random() * (randomArr.length - 1));
 var _link = $('#link')
//  var _resLink = $('#resLink')
 console.log(randNum, 'nnnnnnnnnn')
 _link.find('a').attr('href', randomArr[randNum])
 console.log($('#link').find('a').attr('href'), '00')
 _link.click(function() {
  visitNum(102, 3)
 })
 
  var testData = [
    {
      "test": [
        {
          "title": '1、你是否有吃槟榔的习惯？',
          "option": [
            {id: 1, name: 'A、是'},{id: 1, name: 'B、否'},{id: 1, name: 'C、偶尔会'}
          ]
        },
        {
          "title": '2、你是否有吃着槟榔抽烟的习惯？',
          "option": [
            {id: 2, name: 'A、是'},{id: 2, name: 'B、否'},{id: 2, name: 'C、偶尔会'}
          ]
        },
        {
          "title": '3、你是否有喝老爸茶的习惯？',
          "option": [
            {id: 3, name: 'A、是'},{id: 3, name: 'B、否'},{id: 3, name: 'C、偶尔会'}
          ]
        },
        {
          "title": '4、你是否有晚上吃烧烤的习惯？',
          "option": [
            {id: 4, name: 'A、是'},{id: 4, name: 'B、否'},{id: 4, name: 'C、偶尔会'}
          ]
        },
        {
          "title": '5、你是否有吃定安粉的习惯？',
          "option": [
            {id: 5, name: 'A、是'},{id: 5, name: 'B、否'},{id: 5, name: 'C、偶尔会'}
          ]
        },
        {
          "title": '6、你是否有睡前饮酒的习惯？',
          "option": [
            {id: 6, "last": true, name: 'A、是'},{id: 6, "last": true, name: 'B、否  '},{id: 6, "last": true, name: 'C、偶尔会'}
          ]
        }
      ]
    }
  ]

  $.each(testData,function(i,item){
    var index = i+1;
    if(index == paramId) {
      $("#tpl").tmpl(item).appendTo('#div_demo');
     }
  });

  var lists=$('.group');
  lists.hide();
  lists.first().show();

  var sub = 1
  var _modal = $('.modal')
  var _btn = $('.answer-btn-box')
  var _options = $('.options-item')
  var _result = $('.answer-result')
  var _linkGo = $('.link-go')
  _options.click(function(){
    var _this = $(this);
    paramId = _this.attr('data-index')
    index = _this.attr('data-index')
    _this.addClass('active').siblings().removeClass('active');
    lists.eq(sub).show().siblings().hide();
    sub++
    if(_this.attr('data-last') == 'true') {
      // _resLink.find('a').attr('href', randomArr[randNum])
      _btn.show()
    }
  })

  _btn.click(function(){
    lists.hide();
    _btn.hide();
    _result.show();
    _modal.hide();
    _linkGo.hide()
  })
  _modal.click(function(){
    $(this).hide();
  })

   // 分享功能
   initData()
   var timestamp,nonceStr,signature,appId;
   function initData() {
     var url0 = location.href.split('#').toString();//url不能写死
     $.ajax({
       type:'get',
       url: "https://iwan8.club/wx/jssdk/getConfig", 
       data: { url: url0, oid: '0',attr: '1' },
       success: function(res){
         var data = JSON.parse(res).result
           timestamp = data.timestamp
           nonceStr = data.nonceStr
           signature = data.signature
           appId = data.appid
           wx.config({
             debug: false, // 是否开启调试模式
             appId: appId, //appid
             timestamp: timestamp, // 时间戳
             nonceStr: nonceStr, // 随机字符串
             signature: signature, // 签名
             jsApiList: [
               'onMenuShareTimeline',
               'onMenuShareAppMessage',
             ] // 需要使用的JS接口列表
           })
           wx.ready(function() {
             // 分享朋友圈
             wx.onMenuShareTimeline({
               title: '测一下生活在海南会得什么病？', // 分享标题
               desc: '你也来测测', // 分享描述
               imgUrl: 'https://iwan8.club/wechatRes/res/img/wx/tree.png', // 分享图标
               link: 'https://iwan8.club/wx/4.html?id='+paramId, // 分享链接
               success: function () {
                 lists.hide();
                 _btn.hide();
                 _result.show();
                 _modal.hide();
                 _linkGo.hide()
                 visitNum(102, 2)
               },
               cancel: function () {
                   // 用户取消分享后执行的回调函数
               }
             });
             // 分享给好友
             wx.onMenuShareAppMessage({
             title: '测一下生活在海南会得什么病？', // 分享标题
             desc: '你也来测测', // 分享描述
             link: 'link', // 分享链接
             imgUrl: 'https://iwan8.club/wechatRes/res/img/wx/tree.png', // 分享图标
             link: 'https://iwan8.club/wx/4.html?id='+paramId, // 分享链接
             success: function () {
               lists.hide();
               _btn.hide();
               _result.show();
               _modal.hide();
               _linkGo.hide()
               visitNum(102, 2)
             },
             error: function() {
               alert("失败");
             },
             cancel: function () {
               alert('取消分享')
             }
             })
           })
       },
       error: function() {
       },
     });
   }
   // 访问统计
   visitNum(102, 1)
})